SOURCE_DIR = ../src/phonokit/
DISABLE_COMPLETE ?= no

all: phkit

phkit:
	@echo "Copying Python files ..."
	@cp $(SOURCE_DIR)/*.py .

	@echo "Creating the header ..."
	@echo '#!/usr/bin/env python3' > $@
	@sed -n '/^#/p' phono2.py >> $@

	@echo "Finding all dependencies ..."
	@printf "\n\n" >> $@
	@cat environ.py core.py phono2.py | grep import | awk '!arr[$$0]++' | grep -v -E '_system|environ|core' >> $@

	@echo "Merging environ.py ..."
	@printf "\n\n" >> $@
	@sed -n '/^PKGNAME/,$$p' environ.py | sed 's/_system/os/g' >> $@

	@echo "Merging core.py ..."
	@printf "\n\n" >> $@
	@sed -n '/^@/,$$p' core.py >> $@

	@echo "Merging phono2.py ..."
	@printf "\n\n" >> $@
	@sed -n '/^def/,$$p' phono2.py | sed 's/core\.//g' >> $@

	@echo "Creating the entry point ..."
	@printf "\n\n" >> $@
	@echo 'if __name__ == "__main__":' >> $@
	@echo '    cli()' >> $@

ifeq ($(DISABLE_COMPLETE), yes)
	@echo "Removing 'argcomplete' dependency..."
	@sed -i '/argcomplete/Id' $@
endif

	@echo "Making the output file executable ..."
	@chmod +x $@

	@echo "Cleaning up temporary files ..."
	@rm -f *.py

	@echo "Done!"

clean:
	rm -f *.py phkit

.PHONY: all phkit clean
